<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods,i) in goodsLists" :key="i" @click="gotoDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 请求参数对象
				queryObj: {
					// 查询关键词
					query: '',
					// 商品分类Id
					cid: '',
					// 页码值
					pagenum: 1,
					// 每页显示多少条数据
					pagesize: 10
				},
				goodsList:[],
				total:0,
				//节流阀
				isloading:false
			};
		},
		onLoad(options) {
			
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			console.log(this.queryObj)
			this.getGoodsList()
		},
		methods:{
			//获取商品列表数据
			async getGoodsList(cb){
				//开启节流阀
				this.isloading = true
				const {data:res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				//关闭节流阀
				this.isloading = false
				
				if (res.meta.status !== 200) return uni.$showMsg()
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total
				cb && cb()
			},
			gotoDetail(goods){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
			}
		},
		// 触底的事件
		onReachBottom() {
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('没有数据啦~')
			if(this.isloading) return
		  // 让页码值自增 +1
		  this.queryObj.pagenum += 1
		  // 重新获取列表数据
		  this.getGoodsList()
		},
		onPullDownRefresh() {
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('没有数据啦~')
			if(this.isloading) return
			// 让页码值自增 +1
			this.queryObj.pagenum += 1
			// 重新获取列表数据
			this.getGoodsList(()=>uni.stopPullDownRefresh())
		},
		computed:{
			goodsLists(){
				return [...this.goodsList].reverse()
			}
		}
	}
</script>

<style lang="scss">
	
</style>
